<template>
	<el-dialog
		:title="!dataForm.id ? '新增' : '修改'"
		v-if="isAuth(['ROOT', 'USER:INSERT', 'USER:UPDATE'])"
		:close-on-click-modal="false"
		v-model="visible"
		width="450px"
	>
	<el-scrollbar height="500px">
	    <el-form :model="dataForm" ref="dataForm" :rules="dataRule" label-width="80px">
	        <el-form-item label="药品编号" prop="drugid"><el-input v-model="dataForm.drugid" clearable /></el-form-item>
	        <el-form-item label="药品名称" prop="drugname"><el-input v-model="dataForm.drugname" clearable /></el-form-item>
	        <el-form-item label="药品规格" prop="drugspec"><el-input v-model="dataForm.drugspec" clearable /></el-form-item>
	        <el-form-item label="生产厂家" prop="drugmade"><el-input v-model="dataForm.drugmade" clearable /></el-form-item>
	        <el-form-item label="药品类别">
	            <el-radio-group v-model="dataForm.drugstatus">
	                <el-radio-button label="处方" />
	                <el-radio-button label="非处方" />
	            </el-radio-group>
	        </el-form-item>
			<el-form-item label="药品详情" prop="remark">
				<el-input
					v-model="dataForm.remark"
					type="textarea"
					:rows="8"
					style="width:100%"
					maxlength="500"
					show-word-limit
					clearable
				/>
			</el-form-item>
	    </el-form>
	</el-scrollbar>
	<template #footer>
	    <span class="dialog-footer">
	        <el-button @click="visible = false">取消</el-button>
	        <el-button type="primary" @click="dataFormSubmit">确定</el-button>
	    </span>
	</template>
	</el-dialog>
</template>

<script>
export default {
	data: function() {
		return {
			visible: false,
			
			dataForm: {
			    id: null,
			    drugid: null,
			    drugname: null,
			    drugspec: null,
			    drugmade: null,
			    drugstatus: '处方',
				remark: null
			},
			dataRule: {
			    drugid: [
			        { required: true, message: '药品编号不能为空' },
			        {
			            pattern: '^[0-9]{13}$',
			            message: '药品编号格式错误'
			        }
			    ],
			    drugname: [
			        { required: true, message: '药品名称不能为空' },
			        {
			            pattern: '^[\\u4e00-\\u9fa5]{2,20}$',
			            message: '药品名称格式错误'
			        }
			    ],
			    drugspec: [
			        {
			            required: true,
			            message: '药品规格不能为空'
			        }
			    ],
			    drugmade: [
			        {
			            required: true,
			            message: '生产厂家不能为空'
			        }
			    ],
			    remark: [
			        {
			            required: true,
			            message: '药品详情不能为空'
			        }
			    ]
			}
		};
	},
	methods: {
		reset: function() {
		    let dataForm = {
		        id: null,
		        drugid: null,
		        drugname: null,
		        drugspec: null,
		        drugmade: null,
		        drugstatus: '处方',
		        remark: null
		    };
		    this.dataForm = dataForm;
		},
		init: function(id) {
		    let that = this;
		    //重置表单控件
		    that.reset();
		    //如果id是undefined，就对模型层id变量赋值为0
		    that.dataForm.id = id || 0;
		    //显示对话框
		    that.visible = true;
		    		  
		    //DOM渲染操作要放在$nextTick函数中执行，例如加载数据
		    that.$nextTick(() => {
		        //清理前端验证结果
		        that.$refs['dataForm'].resetFields();
				
				if (that.dataForm.id) {
				    that.$http('/drug/searchById', 'POST', { id: id }, true, function(resp) {
				        let json = {
				            '1': '处方',
				            '2': '非处方'
				        };
				        that.dataForm.drugid = resp.drugid;
				        that.dataForm.drugname = resp.drugname;
				        that.dataForm.drugspec = resp.drugspec;
				        that.dataForm.drugstatus = json[resp.drugstatus + ''];
				        that.dataForm.drugmade = resp.drugmade;
				        that.dataForm.remark = resp.remark;
					});	
				}
		    });
		},
		dataFormSubmit: function() {
		    let that = this;
		    //前端表单验证
		    that.$refs['dataForm'].validate(function(valid) {
		        if (valid) {
		            let json = {
		                处方: 1,
		                非处方: 2
		            };
		            let data = {
		                id: that.dataForm.id,
		                drugid: that.dataForm.drugid,
		                drugname: that.dataForm.drugname,
		                drugspec: that.dataForm.drugspec,
		                drugmade: that.dataForm.drugmade,
		                drugstatus: json[that.dataForm.drugstatus],
						remark: that.dataForm.remark
		            };
		            that.$http(`/drug/${!that.dataForm.id ? 'insert' : 'update'}`, 'POST', data, true, function(
		                resp
		            ) {
		                ElMessage({
		                    message: '操作成功',
		                    type: 'success'
		                });
		                that.visible = false;
		                that.$emit('refreshDataList');
		            });
		        }
		    });
		},
	}
};
</script>

<style lang="less" scoped="scoped"></style>
